package com.esiag.pdsteam.gestion;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;


import javolution.context.ConcurrentContext;
import javolution.util.FastList;
import javolution.util.FastMap;

import com.edu.message.Context;
import com.esiag.pdsteam.main.PileSynchro;
import com.esiag.pdsteam.ordonnancer.Ordonnancer;
import com.esiag.pdsteam.syncro.EPileType;
import com.esiag.pdsteam.syncro.IPileSynchro;
import com.esiag.pdsteam.syncro.PileTypeTuple;

public class InformationInOut implements IPileSynchro,Files,IContextProvider{
	Context context;
	private Map<EPileType, FastList<Information>> _piles = Collections.synchronizedMap(new FastMap<EPileType, FastList<Information>>());

	
	public Map<EPileType, FastList<Information>> get_piles() {
		return _piles;
	}


	public void GererAccesConcurrent()
	{	
		
		FileHandler fileHandler;
		try {
			fileHandler = new FileHandler("GhitaBen.log");

			final Logger logger = Logger.getLogger("loger");
			logger.addHandler( fileHandler);
			SimpleFormatter formatter = new SimpleFormatter();  
            fileHandler.setFormatter(formatter);  
		while(true)
		{
		ConcurrentContext.enter();
		try {
			ConcurrentContext.setConcurrency((Runtime.getRuntime().availableProcessors()/2)-1);
			ConcurrentContext.execute(new Runnable() {
					public void run() {
						logger.log(Level.INFO, "Debut thread1 : " +_piles );
						Information message1 = new Information("BA");
						Information message2 = new Information("NC");
						Information message3 = new Information("LV");
						Information message4 = new Information("LV");
						Information message5 = new Information("MY");
						
						treatMessageIn(EPileType.CRITIC_BA, message1);
						treatMessageIn(EPileType.NON_CRITIC, message2);
						treatMessageIn(EPileType.CRITIC_LV, message3);
						treatMessageIn(EPileType.CRITIC_LV, message4);
						treatMessageIn(EPileType.CRITIC_MY, message5);
						logger.log(Level.INFO, "Fin thread1 : " +_piles );
				}
			});
			
			ConcurrentContext.execute(new Runnable() {
				
				public void run() {
					if(_piles.size()!=0)
					{
					logger.log(Level.INFO, "Debut thread2 : " +_piles );
					
					List<PileTypeTuple> list = new ArrayList<PileTypeTuple>();
					
					list.add(new PileTypeTuple(EPileType.CRITIC_LV, 0));
					list.add(new PileTypeTuple(EPileType.CRITIC_MY, 0));
					list.add(new PileTypeTuple(EPileType.CRITIC_MY, 0));
					list.add(new PileTypeTuple(EPileType.CRITIC_BA, 0));
					list.add(new PileTypeTuple(EPileType.CRITIC_BA, 0));
					list.add(new PileTypeTuple(EPileType.NON_CRITIC, 0));
					list.add(new PileTypeTuple(EPileType.NON_CRITIC, 10));
					new Ordonnancer(list, new PileSynchro());
					
					logger.log(Level.INFO, "Fin thread2 : " +_piles );
					}
					else
					{
						logger.log(Level.INFO, "Pile vide ");
					}
					
				}
			});
		} finally {
			ConcurrentContext.exit();
		}
	
		}
		
		
	} catch (Exception e) {
		e.printStackTrace();
	}
	}


	public void write(EPileType pEntity) {
		synchronized (_piles) {
			Information messageOut = _piles.get(pEntity).removeFirst();
			System.out.println(messageOut);
			if(messageOut != null){
				System.out.println("out : " +messageOut);
					
			}
		}
	}

	public void treatMessageIn(EPileType niveau, Information message)
	{
		synchronized (_piles) {
			FastList<Information> list = _piles.get(niveau);
			if(list == null){
				list = new FastList<Information>();
				_piles.put(niveau, list);
			}
			list.add(message);	
			System.out.println("add : " + list);
		}
	}

	public static InformationInOut getNewInstance(){
		return null;
	}


	public FastList<Information> getFileBA()
	{
		return _piles.get(EPileType.CRITIC_BA);
	}
	
	public FastList<Information> getFileLV()
	{
		return _piles.get(EPileType.CRITIC_LV);
	}
	
	public FastList<Information> getFileMY()
	{
		return _piles.get(EPileType.CRITIC_MY);
	}
	
	public FastList<Information> getFileNC()
	{
		return _piles.get(EPileType.NON_CRITIC);
	}


	public ArrayList<FastList<Serializable>> getContext() {
		// TODO Auto-generated method stub
		return context.getTab();
	}


	public void setContext(ArrayList<FastList<Serializable>> context) {

	}
}
